package com.cssw.fyzb.tps.api.yspay;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cssw.fyzb.tps.api.yspay.utils.*;

//import net.sf.json.JSONObject;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wondream
 * @Date: 2021/1/25 20:26
 */
@Function(description = "银盛支付", parameters = {
        @Parameter(name = "out_trade_no", type = Type.String, description = "外部订单号2021012509142572", required = true),
        @Parameter(name = "shopdate", type = Type.String, description = "日期20210125", required = true),
        @Parameter(name = "total_amount", type = Type.String, description = "交易金额:元", required = true),
        @Parameter(name = "bank_account_no", type = Type.String, description = "银行卡号", required = true),
        @Parameter(name = "bank_account_name", type = Type.String, description = "姓名", required = true),
        @Parameter(name = "bank_account_type", type = Type.String, description = "账号类型personal", required = true),
        @Parameter(name = "bank_name", type = Type.String, description = "开户行", required = true),
        @Parameter(name = "bank_city", type = Type.String, description = "开户网点", required = true)
})
@Component
public class YsPay implements Action {

    @Value("${ysepay.notify}")
    private String notifyUrl;

    @Override
    public Response execute(Request request) throws Exception {
        String outTradeNo = request.getString("out_trade_no");
        String shopdate = request.getString("shopdate");
        String total_amount = request.getString("total_amount");
        String bank_account_no = request.getString("bank_account_no");
        String bank_account_name = request.getString("bank_account_name");
        String bank_account_type = request.getString("bank_account_type");
        String bank_name = request.getString("bank_name");
        String bank_city = request.getString("bank_city");

        Map<String, String> mapData = new HashMap<String, String>();
        JSONObject json = new JSONObject();
        mapData.put("partner_id", "xgy2021");
        mapData.put("timestamp", DateUtil.getDateNow());
        mapData.put("charset", "utf-8");
        mapData.put("sign_type", "RSA");
        mapData.put("version", "3.0");
        mapData.put("notify_url", notifyUrl);
        //查询余额
//        mapData.put("method", "ysepay.online.user.account.get");
//        json.put("user_code", "xgy2021");
//        json.put("user_name", "深圳小羔羊网络科技有限公司");
        //代付接口
        try {
            mapData.put("method", "ysepay.df.single.quick.accept");
//            mapData.put("method", "ysepay.df.single.normal.accept");
            json.set("out_trade_no", outTradeNo);
            json.set("shopdate", shopdate);
            json.set("subject", "1");
            json.set("total_amount", total_amount);
            json.set("timeout_express", "24h");
            json.set("business_code", "2010011");
            json.set("currency", "CNY");
            json.set("bank_account_no", bank_account_no);
            json.set("bank_account_name", bank_account_name);
            json.set("bank_account_type", bank_account_type);
            json.set("bank_card_type", "debit");
            json.set("bank_name", bank_name);
            json.set("bank_city", bank_city);
        } catch (Exception e1) {
            System.out.println("加密时异常");
        }


        try {
            mapData.put("biz_content", json.toString());
            try {
                String sign = YsPaySignUtils.sign(mapData); // 签名
                mapData.put("sign", sign);
            } catch (Exception e) {
                e.printStackTrace();
            }
//查询余额
//            String result = HttpRequest.sendPost("https://openapi.ysepay.com/gateway.do", CommonUtil.mapToString(mapData));
            //代付
            String result = HttpRequest.sendPost("https://df.ysepay.com/gateway.do", CommonUtil.mapToString(mapData));

            if (StringUtils.isBlank(result)) {
                return Response.fail("提现失败");
            }

            boolean resultVerify = false;
            try {
                resultVerify = YsPaySignUtils.resultVerify(result, "ysepay_df_single_quick_accept_response");
//                resultVerify = YsPaySignUtils.resultVerify(result, "ysepay_df_single_normal_accept_response");
            } catch (Exception e) {
                e.printStackTrace();
                return Response.fail("提现失败:配置信息错误");
            }
            System.out.println("请求参数：" + mapData);
            System.out.println("请求返回：" + result);

            System.out.println("同步请求验签结果：" + resultVerify);
            if(!resultVerify){
                return Response.fail("提现失败:配置信息错误");
            }else{
                com.alibaba.fastjson.JSONObject resultJson = JSON.parseObject(result);
                com.alibaba.fastjson.JSONObject responseJson = resultJson.getJSONObject("ysepay_df_single_quick_accept_response"); //快速提现
//                com.alibaba.fastjson.JSONObject responseJson = resultJson.getJSONObject("ysepay_df_single_normal_accept_response"); // 普通提现
                String responseCode = responseJson.getString("code");
                if(responseCode.equals("10000")){
                    return Response.success("提现成功");
                }

                return Response.fail("提现失败").put("errorMsg", responseJson.getString("sub_msg")).put("errorCode", responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail("提现失败");
        }


    }
}
